<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>系统登录</h2>
        <p>欢迎使用管理系统</p>
      </div>
      <el-form 
        :model="loginForm" 
        :rules="loginRules" 
        ref="loginForm"
        class="login-form"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="请输入用户名"
            clearable
            size="large"
            class="login-input"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><user /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            type="password" 
            placeholder="请输入密码"
            show-password
            size="large"
            class="login-input"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleLogin" 
            :loading="loading"
            size="large"
            class="login-button"
            round
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="login-decoration">
      <div class="decoration-circle circle-1"></div>
      <div class="decoration-circle circle-2"></div>
      <div class="decoration-circle circle-3"></div>
    </div>
  </div>
</template>

<script>
import { useAuthStore } from '@/stores/auth'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度至少6位', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    async handleLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          this.loading = true
          try {
            // 使用Pinia store处理登录
            const authStore = useAuthStore()
            const response = await authStore.login({
              username: this.loginForm.username,
              password: this.loginForm.password
            })
            
            // 检查响应是否存在且有code字段
            if (response && response.code === 200) {
              this.$message.success('登录成功')
              // 跳转到首页
              this.$router.push('/main')
            } else {
              // 处理登录失败的情况
              const errorMessage = (response && response.message) || '登录失败'
              this.$message.error(errorMessage)
            }
          } catch (error) {
            // 更好地处理各种错误情况
            console.error('登录错误:', error)
            let errorMessage = '登录失败，请稍后重试'
            
            // 检查是否有响应数据
            if (error && error.message) {
              errorMessage = error.message
            } else if (error && typeof error === 'string') {
              errorMessage = error
            }
            
            this.$message.error(errorMessage)
          } finally {
            this.loading = false
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f0f2f5;
  position: relative;
  overflow: hidden;
}

.login-box {
  width: 100%;
  max-width: 400px;
  padding: 40px 30px;
  background: #ffffff;
  border-radius: 10px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  z-index: 2;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  font-size: 28px;
  color: #333;
  margin-bottom: 10px;
}

.login-header p {
  color: #666;
  font-size: 14px;
}

.login-form {
  padding: 0;
}

.login-input {
  margin-bottom: 20px;
}

.login-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  padding: 10px 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05) !important;
  transition: all 0.3s ease;
  border: 1px solid #e1e5e9;
}

.login-input :deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1) !important;
  border-color: #409eff;
}

.login-input :deep(.el-input__prefix) {
  padding-left: 15px;
}

.login-button {
  width: 100%;
  margin-top: 10px;
  background: #409eff;
  border: none;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  transition: all 0.3s ease;
  border-radius: 8px;
  color: white;
}

.login-button:hover {
  background: #66b1ff;
  transform: translateY(-2px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

.login-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.decoration-circle {
  position: absolute;
  border-radius: 50%;
  background: rgba(64, 158, 255, 0.05);
}

.circle-1 {
  width: 200px;
  height: 200px;
  top: -100px;
  right: -100px;
}

.circle-2 {
  width: 150px;
  height: 150px;
  bottom: -75px;
  left: 100px;
}

.circle-3 {
  width: 100px;
  height: 100px;
  top: 150px;
  left: -50px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .login-box {
    margin: 20px;
    padding: 30px 20px;
  }
  
  .login-header h2 {
    font-size: 24px;
  }
  
  .decoration-circle {
    display: none;
  }
}
</style>